<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #select="{ model, field }">
        <a-popconfirm title="是否新增投入品?" ok-text="是" cancel-text="否" @confirm="handleAdd" @cancel="cancel"
          :open="showModal">
          <JSelectInput placeholder="请选择投入品" :onChange="handleInputChange" v-model:value="model[field]"
            :options="inputList">
          </JSelectInput>
        </a-popconfirm>
      </template>
    </BasicForm>
  </BasicModal>
  <!-- 表单区域 -->
  <TInputsModal @register="registerModal2" @success="handleSuccess"></TInputsModal>
</template>

<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from '../TPurchaseRecord.data';
import { saveOrUpdate } from '../TPurchaseRecord.api';

import { list } from '../../TInputsList/TInputs.api';
import TInputsModal from '../../TInputsList/components/TInputsModal.vue'
import JSelectInput from "/@/components/Form/src/jeecg/components/JSelectInput.vue";
const [registerModal2, { openModal }] = useModal();

// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  //labelWidth: 150,
  schemas: formSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 24 }
});

const inputList = ref([])
const inputsId = ref()
const inputClassId = ref();
const showModal = ref(false)

const getInputList = (inputId: any) => {
  list({ input: inputId || '' }).then((res) => {
    inputList.value = [];
    res.records.map((item: any) => {
      inputList.value.push({
        label: item.name + '--' + item.manufacturer,
        value: item.id
      })
      if (item.name == inputsId.value) {
        setFieldsValue({
          inputsId: item.id
        })
      }
    })
    updateSchema({ field: 'inputsId', componentProps: { options: inputList.value } });
  })
}

// 监听投入品种类变化
const handleChange = (e: any) => {
  inputClassId.value = e
  getInputList(e);
  // updateSchema({ field: 'inputsId', componentProps: { dictCode: "t_inputs,name,id,input="+e } });
}

const handleInputChange = (e: any) => {
  inputsId.value = e;
  if (inputList.value.filter(item => item.value == e).length == 0) {
    showModal.value = true;
  }
}

/**
  * 新增事件
  */
function handleAdd() {
  showModal.value = false;
  openModal(true, {
    isUpdate: false,
    showFooter: true,
    record: { name: inputsId.value }
  });
}

function cancel() {
  showModal.value = false;
}

/**
  * 成功回调
  */
function handleSuccess() {
  (inputList.value = []) && getInputList('');
}

//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  showModal.value = false;
  updateSchema({ field: 'input', componentProps: { onChange: handleChange } });
  updateSchema({ field: 'inputsId', componentProps: { onChange: handleInputChange } });
  getInputList('');
  // updateSchema({ field: 'inputsId', componentProps: { dictCode: "t_inputs,name,id" } });
  //重置表单
  await resetFields();
  setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) {
    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
  }
  // 隐藏底部时禁用整个表单
  setProps({ disabled: !data?.showFooter })
});
//设置标题
const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
//表单提交事件
async function handleSubmit(v) {
  try {
    let values = await validate();
    setModalProps({ confirmLoading: true });
    //提交表单
    await saveOrUpdate(values, isUpdate.value);
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
:deep(.ant-input-number) {
  width: 100%
}

:deep(.ant-calendar-picker) {
  width: 100%
}
</style>
